//
//  AddWordToolView.swift
//  新浪微博项目
//
//  Created by 王炯 on 16/7/27.
//  Copyright © 2016年 王炯. All rights reserved.
//

import UIKit

//定义一个按钮的枚举(定义枚举不加逗号）
enum buttonType:Int
{
    case pic = 1001
    case emention = 1002
    case trend = 1003
    case emotion = 1004
    case add = 1005
}

class AddWordToolView: UIView
{

    
    //声明一个Bool值来从外面改变表情按钮的image
    var isEmotion:Bool = false{
        
        
        didSet{
            
            if isEmotion
            {
                //如果下面显示是是表情键盘
                //button点击图标设置为键盘
                self.emotionButton.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                
                self.emotionButton.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
                
                
                
            }
            else
            {
                //如果是系统键盘
                //button点击图标设置为表情
            self.emotionButton.setImage(UIImage(named:"compose_emoticonbutton_background"), forState: UIControlState.Normal)
                self.emotionButton.setImage(UIImage(named:"compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }
            
        }
        
    }
    

    
    
    
    //声明一个闭包，传递button的type
    var clickClosure:((type:buttonType) -> ())?
    
    
    
    //懒加载5个按钮
    //1-图片
    private lazy var pictureButton:UIButton = {
       
        let btn = UIButton()
        
        btn.setImage(UIImage(named:"compose_toolbar_picture"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named:"compose_toolbar_picture_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        //设置button的tag
        btn.tag = 1001
        
        //添加点击事件
        btn.addTarget(self, action: #selector(AddWordToolView.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    //2-@
    private lazy var ementionButton:UIButton = {
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named:"compose_mentionbutton_background"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named:"compose_mentionbutton_background_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        //设置button的tag
        btn.tag = 1002
        
        //添加点击事件
        btn.addTarget(self, action: #selector(AddWordToolView.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return btn
    }()
    
    //3-#
    private lazy var trendButton:UIButton = {
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named:"compose_trendbutton_background"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named:"compose_trendbutton_background_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        //设置button的tag
        btn.tag = 1003
        
        //添加点击事件
        btn.addTarget(self, action: #selector(AddWordToolView.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    //4-emotion
    private lazy var emotionButton:UIButton = {
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named:"compose_emoticonbutton_background"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named:"compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        //设置button的tag
        btn.tag = 1004
        
        //添加点击事件
        btn.addTarget(self, action: #selector(AddWordToolView.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    //5-+
    private lazy var addButton:UIButton = {
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named:"compose_add_background"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named:"compose_add_background_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        //设置button的tag
        btn.tag = 1005
        
        //添加点击事件
        btn.addTarget(self, action: #selector(AddWordToolView.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        
        //自定义界面
        setUpUI()
        
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //自定义界面
    func setUpUI()
    {
        self.backgroundColor = RandomColor()
        
        //添加控件
        addSubview(pictureButton)
        addSubview(ementionButton)
        addSubview(trendButton)
        addSubview(emotionButton)
        addSubview(addButton)
        
        //设置约束
        //上，左，下，与第二按钮宽相等
        pictureButton.snp_makeConstraints { (make) in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(self.ementionButton)
        }
        
        //上，下，左，与第三个按钮宽相等
        ementionButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(self.trendButton)
        }
        
        //上，下，左，与第四个按钮宽相等
        trendButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(ementionButton.snp_trailing)
            make.width.equalTo(self.emotionButton)
        }
        
        //上，下，左，与第五个按钮宽相等
        emotionButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(self.addButton)
        }
        
        //上，下，左，右
        addButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(emotionButton.snp_trailing)
            make.trailing.equalTo(self.snp_trailing)
        }
    }
    
    //添加5个按钮的共同点击事件
    func buttonClick(button: UIButton)
    {
        
        //将传入的button的tag值转换成buttonType
        let type:buttonType = buttonType(rawValue: button.tag)!
        
        
        //点击按钮，调用闭包
        self.clickClosure?(type: type)
        
        
    }

}
